import omit from 'lodash/omit'
import {
  ChooseInternals,
  type ChooseContact as ChooseInternal,
} from '@/desktop/components/choose-internals'
import type { PersonCardWord } from '@/services/common/definition'
import './index.module.less'

export default function EditPersonCard(props: {
  word: PersonCardWord
  onSave: (item: PersonCardWord) => void
  onCancel: () => void
}) {
  const { word, onSave, onCancel } = props

  const choosed = word.user_id ? word : null

  const save = useCallback(
    (choosed: ChooseInternal[]) => {
      if (!choosed.length) return
      const contact = choosed[0]!
      onSave({ ...word, ...omit(contact, 'userId'), user_id: contact.userId })
    },
    [word, onSave],
  )

  return <ChooseInternals
    choosed={choosed ? [{ ...omit(choosed, 'user_id'), userId: choosed.user_id }] : []}
    onChoosed={save}
    onCancel={onCancel}
    tips={
      <div css={{ color: '#aaa', paddingTop: '12px', fontSize: '13px' }}>
        注意：仅能发送相同企业下的员工名片
      </div>
    }
    single
  />
}
